﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Threading;
using System.Globalization;
using System.Data;
using System.Data.SqlClient;

public partial class InternalHistory : System.Web.UI.Page
{
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {

            SqlDataSource1.SelectParameters["internalID"].DefaultValue = null;
            SqlDataSource1.SelectParameters["company_product_id"].DefaultValue = Request.QueryString["company_product"].ToString();
            SqlDataSource1.SelectParameters["delivered"].DefaultValue = null;
            SqlDataSource1.SelectParameters["userid"].DefaultValue = null;
            SqlDataSource1.SelectParameters["fromcopyno"].DefaultValue = null;
            SqlDataSource1.SelectParameters["tocopyno"].DefaultValue = null;
            SqlDataSource1.SelectParameters["fromdate"].DefaultValue = null;
            SqlDataSource1.SelectParameters["todate"].DefaultValue = null;


            ViewState["internalID"] = SqlDataSource1.SelectParameters["internalID"].DefaultValue;
            ViewState["company_product_id"] = SqlDataSource1.SelectParameters["company_product_id"].DefaultValue;
            ViewState["delivered"] = SqlDataSource1.SelectParameters["delivered"].DefaultValue;
            ViewState["userid"] = SqlDataSource1.SelectParameters["userid"].DefaultValue;
            ViewState["fromcopyno"] = SqlDataSource1.SelectParameters["fromcopyno"].DefaultValue;
            ViewState["tocopyno"] = SqlDataSource1.SelectParameters["tocopyno"].DefaultValue;
            ViewState["fromdate"] = SqlDataSource1.SelectParameters["fromdate"].DefaultValue;
            ViewState["todate"] = SqlDataSource1.SelectParameters["todate"].DefaultValue;

            GridView1.DataBind();
        }
       
    }
    protected override void InitializeCulture()
    {
        if (Session["CurrentCulture"] != null)
        {
            string lang = Session["CurrentCulture"].ToString();
            if (lang != null && lang != "")
            {
                Thread.CurrentThread.CurrentUICulture = new CultureInfo(lang);
                Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(lang);
                Thread.CurrentThread.CurrentCulture.DateTimeFormat = new CultureInfo("en-US", false).DateTimeFormat;
                // Thread.CurrentThread.CurrentCulture.Calendar=new  System.Globalization.GregorianCalendar();

            }
        }
    }

    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        
        //if (e.Row.RowType == DataControlRowType.DataRow)
        //{
        //    e.Row.Attributes.Add("onmouseover", "this.Mystyle=this.style.backgroundColor;this.style.backgroundColor='#FFFFA8'");

        //    e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=this.Mystyle");
        //}
    }
    protected void save_Click(object sender, EventArgs e)
    {
        if (currentsubs.SelectedValue != "0")
        {
            SqlDataSource1.SelectParameters["internalID"].DefaultValue = currentsubs.SelectedValue;
            SqlDataSource1.SelectParameters["company_product_id"].DefaultValue = Request.QueryString["company_product"];
            SqlDataSource1.SelectParameters["delivered"].DefaultValue = null;
            SqlDataSource1.SelectParameters["userid"].DefaultValue = null;
            if (DatePicker1.SelectedDate.Year.ToString() != "1")
                SqlDataSource1.SelectParameters["fromdate"].DefaultValue = DatePicker1.SelectedDate.Date.ToString("yyyy-MM-dd");
            else
                SqlDataSource1.SelectParameters["fromdate"].DefaultValue = null;

            if (DatePicker2.SelectedDate.Year.ToString() != "1")
                SqlDataSource1.SelectParameters["todate"].DefaultValue = DatePicker2.SelectedDate.Date.ToString("yyyy-MM-dd");
            else
                SqlDataSource1.SelectParameters["todate"].DefaultValue = null;

            if (string.IsNullOrEmpty(RadioButtonList1.SelectedValue))
            {
                SqlDataSource1.SelectParameters["fromcopyno"].DefaultValue = null;
                SqlDataSource1.SelectParameters["tocopyno"].DefaultValue = null;

            }
            else if(RadioButtonList1.SelectedValue=="0")
            {
                SqlDataSource1.SelectParameters["fromcopyno"].DefaultValue = "0";
                SqlDataSource1.SelectParameters["tocopyno"].DefaultValue = "0";
                SqlDataSource1.SelectParameters["delivered"].DefaultValue = "false";
            }
            else if (RadioButtonList1.SelectedValue == "1")
            {
                SqlDataSource1.SelectParameters["fromcopyno"].DefaultValue = "1";
                SqlDataSource1.SelectParameters["tocopyno"].DefaultValue = null;
                SqlDataSource1.SelectParameters["delivered"].DefaultValue = "true";
            }


            ViewState["internalID"] = SqlDataSource1.SelectParameters["internalID"].DefaultValue;
            ViewState["company_product_id"] = SqlDataSource1.SelectParameters["company_product_id"].DefaultValue;
            ViewState["delivered"] = SqlDataSource1.SelectParameters["delivered"].DefaultValue;
            ViewState["userid"] = SqlDataSource1.SelectParameters["userid"].DefaultValue;
            ViewState["fromcopyno"] = SqlDataSource1.SelectParameters["fromcopyno"].DefaultValue;
            ViewState["tocopyno"] = SqlDataSource1.SelectParameters["tocopyno"].DefaultValue;
            ViewState["fromdate"] = SqlDataSource1.SelectParameters["fromdate"].DefaultValue;
            ViewState["todate"] = SqlDataSource1.SelectParameters["todate"].DefaultValue;
            
            GridView1.DataBind();
            details.Visible = true;

        }
        else
        {
            details.Visible = false;
        }
      
        
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {

        
    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
       string  internalID = SqlDataSource1.SelectParameters["internalID"].DefaultValue;
       string  company_product_id = SqlDataSource1.SelectParameters["company_product_id"].DefaultValue;
       string  delivered= SqlDataSource1.SelectParameters["delivered"].DefaultValue;
       string  userid = SqlDataSource1.SelectParameters["userid"].DefaultValue;
       string  fromcopyno= SqlDataSource1.SelectParameters["fromcopyno"].DefaultValue;
       string tocopyno = SqlDataSource1.SelectParameters["tocopyno"].DefaultValue;
      string  fromdate = SqlDataSource1.SelectParameters["fromdate"].DefaultValue;
      string todate = SqlDataSource1.SelectParameters["todate"].DefaultValue;

        
         
        string redirect="";
        if (ViewState["filter"] == null)
        {
            Session["data"] = DB.getIntHistoryrep(SqlDataSource1.SelectCommand, internalID, company_product_id,delivered,userid,fromcopyno,tocopyno,fromdate , todate);
            redirect = "~/reps2.aspx?rep=" + "InternalHisRep";

           // InternalHistories ints = DB.getIntHistoryrep(SqlDataSource1.SelectCommand, ViewState["mypro"].ToString(), ViewState["myid"].ToString());
        }
        
        
        Response.Redirect(redirect);
       

    }

    protected void currentsubs_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (currentsubs.SelectedValue != "0")
        {
            SqlDataSource1.SelectParameters["internalID"].DefaultValue = currentsubs.SelectedValue;
            SqlDataSource1.SelectParameters["company_product_id"].DefaultValue = Request.QueryString["company_product"];
            SqlDataSource1.SelectParameters["delivered"].DefaultValue = null;
            SqlDataSource1.SelectParameters["userid"].DefaultValue = null;

            GridView1.DataBind();
            details.Visible = true;

        }
        else
        {
            details.Visible = false;
        }
    }

    protected void GridView1_DataBound(object sender, EventArgs e)
    {
        DataTable dt = ((DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty)).Table;
        string sumarrive = dt.Compute("sum(actualcopyno)", "").ToString();
        arrive.Text = sumarrive == "" ? "0" : sumarrive;
       

        string sumnoarrive = dt.Compute("sum(assumedcopyno)", "actualcopyno = 0").ToString();

        noarrive.Text = sumnoarrive == "" ? "0" : sumnoarrive;
    }

    
}
